<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_geometryBatchAnalystService"></title>
    <script type="text/javascript" src="../js/include-web.js"></script>
    <script type="text/javascript" src="../../dist/openlayers/include-openlayers.js"></script>
    <style>
        .ol-popup {
            position: absolute;
            background-color: white;
            -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
            filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
            padding: 15px;
            border-radius: 10px;
            border: 1px solid #cccccc;
            bottom: 12px;
            left: -50px;
            min-width: 120px;
        }

        .ol-popup:after, .ol-popup:before {
            top: 100%;
            border: solid transparent;
            content: " ";
            height: 0;
            width: 0;
            position: absolute;
            pointer-events: none;
        }

        .ol-popup:after {
            border-top-color: white;
            border-width: 10px;
            left: 48px;
            margin-left: -10px;
        }

        .ol-popup:before {
            border-top-color: #cccccc;
            border-width: 11px;
            left: 48px;
            margin-left: -11px;
        }
    </style>
</head>
<body style="margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<div id="popup" class="ol-popup">
    <div id="popup-content"></div>
</div>
<script type="text/javascript">
    var host = window.isLocal ? window.server : "http://support.supermap.com.cn:8090";
    var map,
        baseUrl = host + "/iserver/services/map-jingjin/rest/maps/京津地区地图",
        serviceUrl = host + "/iserver/services/spatialanalyst-sample/restjsr/spatialanalyst";
    var extent = [104.07, 30.54, 119.51, 42.31],
        container = document.getElementById('popup'),
        content = document.getElementById('popup-content'),
        overlay = new ol.Overlay(({
            element: container,
            autoPan: true,
            autoPanAnimation: {
                duration: 250
            },
            offset: [0, -5]
        }));
    new ol.supermap.MapService(baseUrl).getMapInfo(function (serviceResult) {
        var mapJSONObj = serviceResult.result;
        map = new ol.Map({
            target: 'map',
            controls: ol.control.defaults({attributionOptions: {collapsed: false}})
                .extend([new ol.supermap.control.Logo()]),
            view: new ol.View({
                center: [116.85, 39.79],
                zoom: 9,
                projection: "EPSG:4326",
                extent: extent
            }),
            overlays: [overlay]
        });
        options = ol.source.TileSuperMapRest.optionsFromMapJSON(baseUrl, mapJSONObj);
        var layer = new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest(options)
        });
        map.addLayer(layer);
        batchAnalystFromGeometry();
    });

    function batchAnalystFromGeometry() {
        //缓冲区分析数据：
        var bufferLine = {
            "type": "Feature",
            "geometry": {
                "type": "LineString",
                "coordinates": [[117, 40.50], [118, 40]]
            }
        };
        var bufferPoint = {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [117, 40]
            }
        };
        //叠加分析数据：
        var sourceGeometry = {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [[[116, 39.75],
                    [116, 39.15],
                    [117, 39.15],
                    [117, 39.85],
                    [116, 39.85]]]
            }
        };
        var operateGeometry = {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [[[116.25, 40.5],
                    [116.25, 38.5],
                    [116.75, 38.5],
                    [116.75, 40.5],
                    [116.25, 40.5]]]
            }
        };

        //数据加载到地图：
        var geojsonFeature = {
            "type": "FeatureCollection",
            "features": [bufferLine, bufferPoint, sourceGeometry, operateGeometry]
        };
        var styles = {
            'Point': new ol.style.Style({
                image: new ol.style.Circle({
                    radius: 4,
                    fill: new ol.style.Fill({
                        color: 'rgba(0,0,255)'
                    }),
                    stroke: new ol.style.Stroke({color: 'blue', width: 1.5})
                })
            }),
            'Polygon': new ol.style.Style({
                stroke: new ol.style.Stroke({
                    color: 'blue',
                    width: 1
                }),
                fill: new ol.style.Fill({
                    color: 'rgba(174, 238, 238, 0.3)'
                })
            }),
            'LineString': new ol.style.Style({
                stroke: new ol.style.Stroke({
                    color: 'blue',
                    width: 1.5
                })
            })
        };

        var styleFunction = function (feature) {
            return styles[feature.getGeometry().getType()];
        };
        var overlaySource = new ol.source.Vector({
            features: (new ol.format.GeoJSON()).readFeatures(geojsonFeature)
        });

        var overlayLayer = new ol.layer.Vector({
            source: overlaySource,
            style: styleFunction
        });
        map.addLayer(overlayLayer);

        //缓冲区分析参数
        var geoBufferAnalystParams = {
            analystName: "buffer",
            param: new SuperMap.GeometryBufferAnalystParameters({
                sourceGeometry: bufferLine,
                sourceGeometrySRID: 4326,
                bufferSetting: new SuperMap.BufferSetting({
                    endType: SuperMap.BufferEndType.ROUND,
                    leftDistance: new SuperMap.BufferDistance({value: 5000}),
                    rightDistance: new SuperMap.BufferDistance({value: 5000}),
                    semicircleLineSegment: 10
                })

            })
        };
        var geoBufferAnalystParams_Point = {
            analystName: "buffer",
            param: new SuperMap.GeometryBufferAnalystParameters({
                sourceGeometry: bufferPoint,
                sourceGeometrySRID: 4326,
                bufferSetting: new SuperMap.BufferSetting({
                    endType: SuperMap.BufferEndType.ROUND,
                    leftDistance: new SuperMap.BufferDistance({value: 5000}),
                    rightDistance: new SuperMap.BufferDistance({value: 5000}),
                    radiusUnit: "METER",
                    semicircleLineSegment: 10
                })

            })
        };

        //叠加分析参数
        var OverlayBatchAnalystParameters = {
            analystName: "overlay",
            param: new SuperMap.GeometryOverlayAnalystParameters({
                sourceGeometry: sourceGeometry,
                operateGeometry: operateGeometry,
                operation: SuperMap.OverlayOperationType.CLIP
            })
        };

        //批量分析参数
        var paramter = [geoBufferAnalystParams, OverlayBatchAnalystParameters, geoBufferAnalystParams_Point];

        //批量分析
        new ol.supermap.SpatialAnalystService(serviceUrl).geometrybatchAnalysis(paramter, function (serviceResult) {
            //结果展示
            serviceResult.result[0].resultGeometry.properties.analystType = "buffer_line";
            serviceResult.result[1].resultGeometry.properties.analystType = "overlay";
            serviceResult.result[2].resultGeometry.properties.analystType = "buffer_point";
            var results = [];
            serviceResult.result.map(function (itme) {
                results.push(itme.resultGeometry)
            });
            var resultSource = new ol.source.Vector({
                features: (new ol.format.GeoJSON()).readFeatures({
                    "type": "FeatureCollection",
                    "features": results
                })
            });
            var resultLayer = new ol.layer.Vector({
                source: resultSource,
                style: new ol.style.Style({
                    stroke: new ol.style.Stroke({
                        color: 'red',
                        width: 1.5
                    }),
                    fill: new ol.style.Fill({
                        color: 'rgba(255, 0, 0, 0.3)'
                    })
                })
            });
            map.addLayer(resultLayer);
            map.on('pointermove', function (e) {
                var coordinates = e.coordinate;
                var isShowPop = false;
                map.forEachFeatureAtPixel(e.pixel, function (feature) {
                    if (feature.getProperties().analystType === "buffer_line") {
                        map.getTargetElement().style.cursor = 'pointer';
                        var contentHTML = resources.text_bufferAnalystResult;
                        content.innerHTML = contentHTML;
                        overlay.setPosition([117.5, 40.25]);
                        map.addOverlay(overlay);
                        isShowPop = true;
                    } else if (feature.getProperties().analystType === "buffer_point") {
                        map.getTargetElement().style.cursor = 'pointer';
                        var contentHTML = resources.text_bufferAnalystResult;
                        content.innerHTML = contentHTML;
                        overlay.setPosition([117, 40]);
                        map.addOverlay(overlay);
                        isShowPop = true;
                    } else if (feature.getProperties().analystType === "overlay") {
                        map.getTargetElement().style.cursor = 'pointer';
                        var contentHTML = resources.text_overlayAnalystResult;
                        content.innerHTML = contentHTML;
                        overlay.setPosition([116.5, 39.5]);
                        map.addOverlay(overlay);
                        isShowPop = true;
                    }
                }, {
                    hitTolerance: 10
                });
                if (isShowPop) {
                    return;
                } else {
                    map.getTargetElement().style.cursor = '';
                    overlay.setPosition(undefined);
                    map.removeOverlay(overlay);
                }
            });
        });
    }
</script>
</body>
</html>